package com.mofei.health.check.util;

import org.bouncycastle.math.ec.ECPoint;
import org.pzone.crypto.SM2;
import java.io.*;
import java.math.BigInteger;

/**
 * @author guofei
 */
public class EncryptFile {
    private static final String DIR = System.getProperty("user.dir");

    public static void main(String[] args) throws FileNotFoundException {
        long l = System.currentTimeMillis();
        File file = new File(DIR + "/data/reports/110101199012013210.pdf");
        byte[] originFileData = readFileData(file);
        System.out.println("读取原始文件内容，耗时：" + (System.currentTimeMillis() - l) + " ms");

        long l1 = System.currentTimeMillis();
        byte[] encodeFileData = encodeFileData(originFileData);
        File encodeFile = new File(DIR + "/data/encrypt_reports/" + file.getName());
        writeFileData(encodeFileData, encodeFile);
        System.out.println("加密原始内容并写入新文件，耗时：" + (System.currentTimeMillis() - l1) + " ms");

        long l2 = System.currentTimeMillis();
        byte[] decodeFileData = decodeFileData(encodeFile);
        File decodeFile = new File(DIR + "/data/decrypt_reports/" + file.getName());
        writeFileData(decodeFileData, decodeFile);
        System.out.println("解密文件并写入新文件，耗时：" + (System.currentTimeMillis() - l2) + " ms");

    }

    /**
     * 用私钥解密文件数据
     *
     * @param file
     * @throws FileNotFoundException
     */
    private static byte[] decodeFileData(File file) throws FileNotFoundException {
        byte[] bytes = readFileData(file);
        SM2 sm2 = new SM2();
        BigInteger privateKey = sm2.importPrivateKey(DIR + "/data/keystore/privatekey.pem");
        return sm2.decrypt(bytes, privateKey);
    }

    /**
     * 用公钥加密文件数据
     *
     * @param bytes
     * @return
     */
    private static byte[] encodeFileData(byte[] bytes) {
        SM2 sm2 = new SM2();
        ECPoint publicKey = sm2.importPublicKey(DIR + "/data/keystore/publickey.pem");
        return sm2.encrypt(bytes, publicKey);
    }

    /**
     * 读文件
     *
     * @param file
     * @return
     * @throws FileNotFoundException
     */
    private static byte[] readFileData(File file) throws FileNotFoundException {
        if (!file.exists()) {
            throw new FileNotFoundException("file not found");
        }
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new byte[0];
    }

    /**
     * 写文件
     *
     * @param bytes
     * @param file
     */
    private static void writeFileData(byte[] bytes, File file) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
